package groupOne.app.DWS;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;
import groupOne.app.BaseAppOneStream;
import groupOne.bean.SourceOrderBean;
import groupOne.common.Constant;
import groupOne.util.AtguiguUtil;
import groupOne.util.DimAsyncFunction;
import groupOne.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * use edu;
 *
 * drop table if exists dws_trade_source_order_window;
 * create table if not exists dws_trade_source_order_window(
 *       stt datetime,
 *       edt datetime,
 *       cur_date date,
 *       sc_id varchar(2000),
 *       sc_site varchar(2000),
 *       sc_url varchar(2000),
 *
 *       total_amount decimal replace,
 *       uu_ct bigint replace,
 *       order_ct bigint replace
 * )
 *     engine = olap
 *     aggregate key(stt,edt,cur_date,sc_id,sc_site,sc_url)
 * comment "考试域答题汇总表"
 * partition by range(cur_date)()
 * distributed by hash(stt) buckets 10 properties(
 *     "replication_num" = "3",
 *   "dynamic_partition.enable" = "true",
 *   "dynamic_partition.time_unit" = "DAY",
 *   "dynamic_partition.start" = "-1",
 *   "dynamic_partition.end" = "1",
 *   "dynamic_partition.prefix" = "par",
 *   "dynamic_partition.buckets" = "10",
 *   "dynamic_partition.hot_partition_num" = "3"
 * );
 */
public class Dws_TradeSourceOrderWindowXiang extends BaseAppOneStream {
    public static void main(String[] args) {
        new Dws_TradeSourceOrderWindowXiang().init(
                6010,
                2,
                "Dws_TradeSourceOrderWindowXiang",
                Constant.TOPIC_DWD_SOURCE_ORDER
        );
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        //封装pojo
        DataStream<SourceOrderBean> pojoStream = transFunc(stream);
        //聚合
        SingleOutputStreamOperator<SourceOrderBean> aggStream = aggFunc(pojoStream);

        //dim
        SingleOutputStreamOperator<SourceOrderBean> addDimStream = addDim(aggStream);
        //写入doris
        writeDoris(addDimStream);
    }

    private SingleOutputStreamOperator<SourceOrderBean> addDim(SingleOutputStreamOperator<SourceOrderBean> aggStream) {
        return AsyncDataStream.unorderedWait(
                aggStream,
                new DimAsyncFunction<SourceOrderBean>() {
                    @Override
                    public String getTable() {
                        return "dim_base_source";
                    }

                    @Override
                    public String getId(SourceOrderBean input) {
                        return input.getScId();
                    }

                    @Override
                    public void addDim(JSONObject dim, SourceOrderBean bean) {
                    bean.setScSite(dim.getString("SOURCE_SITE"));
                    bean.setScUrl(dim.getString("SOURCE_URL"));
                    }
                },
                60,
                TimeUnit.SECONDS
        );
    }

    private void writeDoris(SingleOutputStreamOperator<SourceOrderBean> aggStream) {
        aggStream
                .map(bean -> {
                    SerializeConfig config = new SerializeConfig();
                    config.propertyNamingStrategy= PropertyNamingStrategy.SnakeCase;
                    return JSON.toJSONString(bean,config);
                })
                .addSink(FlinkSinkUtil.getDorisSink("edu.dws_trade_source_order_window"));
    }

    private SingleOutputStreamOperator<SourceOrderBean> aggFunc(DataStream<SourceOrderBean> pojoStream) {
        return pojoStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<SourceOrderBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((t,l)->t.getTs())
                )
                .keyBy(SourceOrderBean::getScId)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<SourceOrderBean>() {
                            @Override
                            public SourceOrderBean reduce(SourceOrderBean bean1, SourceOrderBean bean2) throws Exception {
                                bean1.setTotalAmount(bean1.getTotalAmount().add(bean2.getTotalAmount()));
                                bean1.setUuCt(bean1.getUuCt() + bean2.getUuCt());
                                bean1.setOrderCt(bean1.getOrderCt() + bean2.getOrderCt());
                                return bean1;
                            }
                        },
                        new ProcessWindowFunction<SourceOrderBean, SourceOrderBean, String, TimeWindow>() {
                            @Override
                            public void process(String scId,
                                                ProcessWindowFunction<SourceOrderBean, SourceOrderBean, String, TimeWindow>.Context ctx,
                                                Iterable<SourceOrderBean> iterable,
                                                Collector<SourceOrderBean> collector) throws Exception {
                                SourceOrderBean bean = iterable.iterator().next();
                                bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));
                                bean.setCurDate(AtguiguUtil.toDate(ctx.window().getStart()));

                                collector.collect(bean);

                            }
                        });
    }

    private DataStream<SourceOrderBean> transFunc(DataStreamSource<String> stream) {
        SingleOutputStreamOperator<SourceOrderBean> notUuCtStream = stream
                .map(new MapFunction<String, SourceOrderBean>() {
                    @Override
                    public SourceOrderBean map(String json) throws Exception {
                        JSONObject obj = JSON.parseObject(json);
                        String sc_id = obj.getString("sc");
                        BigDecimal finalAmount = obj.getBigDecimal("final_amount");
                        Long ts = obj.getLong("ts") * 1000;
                        return SourceOrderBean.builder()
                                .scId(sc_id)
                                .totalAmount(finalAmount)
                                .orderCt(1l)
                                .uuCt(0l)
                                .ts(ts)
                                .build();
                    }
                });
        SingleOutputStreamOperator<SourceOrderBean> uuStream = stream
                .map(JSON::parseObject)
                .keyBy(obj -> obj.getString("sc") + ":" + obj.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, SourceOrderBean>() {

                    private ValueState<String> lastState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        lastState = getRuntimeContext().getState(new ValueStateDescriptor<String>("lastState", String.class));
                    }

                    @Override
                    public void processElement(JSONObject obj,
                                               KeyedProcessFunction<String, JSONObject, SourceOrderBean>.Context ctx,
                                               Collector<SourceOrderBean> collector) throws Exception {
                        String lastDay = lastState.value();
                        String sc_id = obj.getString("sc");
                        Long ts = obj.getLong("ts") * 1000;
                        String today = AtguiguUtil.toDate(ts);
                        if (!today.equals(lastDay)) {
                            lastState.update(today);
                            collector.collect(SourceOrderBean.builder()
                                    .scId(sc_id)
                                    .totalAmount(new BigDecimal(0))
                                    .orderCt(0l)
                                    .uuCt(1l)
                                    .ts(ts)
                                    .build());
                        }

                    }
                });
        return notUuCtStream.union(uuStream);

    }
}
